package cn.auction.web;

import cn.auction.biz.IuseBiz;
import cn.auction.biz.impl.IuserBizImpl;
import cn.auction.entity.AuctionUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

public class UserController {
    IuseBiz ibiz=new IuserBizImpl();
    public String insertRegin(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        AuctionUser auctionUser=new AuctionUser();
        auctionUser.setUserName(request.getParameter("username"));
        auctionUser.setUserPassword(request.getParameter("userpassword"));
        auctionUser.setUserCardNo(request.getParameter("usercardno"));
        auctionUser.setUserTel(request.getParameter("usertel"));
        auctionUser.setUserAddress(request.getParameter("useraddress"));
        auctionUser.setUserPostNumber(request.getParameter("userpostnumber"));
        auctionUser.setUserIsAdmin(false);
        request.setAttribute("registerUser",auctionUser);
        Object numrand=request.getSession().getAttribute("numrand");
        if(numrand==null||!numrand.equals(request.getParameter("inputCode"))){
            return "/register.jsp?msg=validateCodeError";
        }else{

            ibiz.insert(auctionUser);
            return "redirect:/login.jsp?msg=registerSuccess";
        }
    }
    public String doLogin(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String userName=request.getParameter("username");
        String userPassWord=request.getParameter("userpassword");
        request.setAttribute("username",userName);
        request.setAttribute("userpassword",userPassWord);
        if(!request.getSession().getAttribute("numrand").equals(request.getParameter("inputCode"))){
            return "forward:/login.jsp?msg=validateCodeError";
        }else{
            AuctionUser auctionUser=ibiz.UserLogin(userName,userPassWord,false);
            if(auctionUser==null){
                return "/login.jsp?msg=loginError";
            }else{
                request.getSession().removeAttribute("auctionUserNew");
                request.getSession().setAttribute("user",auctionUser);
                return "redirect:/do/auction/auctionList";
            }
        }
    }
    //管理员登录
    public String adminLogin(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String userName=request.getParameter("username");
        String userPassWord=request.getParameter("userpassword");
        if(!request.getSession().getAttribute("numrand").equals(request.getParameter("inputCode"))){
            return "forward:/login.jsp?msg=validateCodeError";
        }else{
            AuctionUser auctionUser=ibiz.UserLogin(userName,userPassWord,true);
            request.getSession().setAttribute("auctionUserNew",auctionUser);
            System.out.println("123456");
            if(auctionUser.getUserIsAdmin()==true){
                return "redirect:/do/auction/auctionList";
            }else{
                return "login-admin.jsp";
            }
        }

    }
    //用户注销
    public String UserExit(HttpServletRequest request,HttpServletResponse response){
        System.out.println("888888888888888888");
        request.getSession().removeAttribute("user");
        request.getSession().removeAttribute("auctionUserNew");
        return "redirect:/login.jsp";
    }

}
